package com.atguigu.jxc.controller;

import com.alibaba.fastjson.JSON;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.service.GoodsService;
import com.google.gson.Gson;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @description 商品信息Controller
 */
@RequestMapping("/goods")
@Api(tags = "首页商品查询模块")
@RestController
public class GoodsController {

    @Autowired
    private GoodsService goodsService;
    @ApiOperation("分页查询客户信息")
    @PostMapping("list")
    public String queryPageGoodsNoCondition(@RequestParam("page")Integer page, @RequestParam("rows")Integer rows,
                                            @RequestParam(value = "goodsName",required = false)String goodsName,
                                            @RequestParam(value = "goodsTypeId",required = false)String goodsTypeId){

        Map<String,Object> map = goodsService.queryPageGoodsByNameAndTypeId(page,rows,goodsName,goodsTypeId);
        Gson gson = new Gson();
        return gson.toJson(map);
    }
    /**
     * 分页查询商品库存信息
     * @param page 当前页
     * @param rows 每页显示条数
     * @param codeOrName 商品编码或名称
     * @param goodsTypeId 商品类别ID
     * @return
     */
    //下面这个方式接收参数不行,swagger2里面测试可以,但是前端里面测试会出现415, codeOrName=%E5%B9%B2&goodsTypeId=6&page=1&rows=30以路径的方式传参的
    @PostMapping("listInventory")
    @ApiOperation("根据商品类别,商品编码或者名称搜索")
    /* public String queryGoodsInventory(@RequestBody GoodsSearchVo goodsSearchVo){
        Map<String,Object> map = goodsService.queryPageGoods(goodsSearchVo);
        return JSON.toJSONString(map);
    } */
    public String queryGoodsInventory(@RequestParam("page")Integer page,@RequestParam("rows")Integer rows,
                                      //一进入页面的时候,会发送一个请求查询,但是这个请求参数里面没有codeOrName和goodsTypeId
                                      @RequestParam(value = "codeOrName",required = false)String codeOrName,
                                      @RequestParam(value = "goodsTypeId",required = false)Integer goodsTypeId){
        //测试一下自己写的分页的功能好不好用
        // rows = 10;
        Map<String,Object> map = goodsService.queryPageGoods(page,rows,codeOrName,goodsTypeId);
        return JSON.toJSONString(map);
    }

    @ApiOperation("商品的添加或修改")
    @PostMapping("save")
    public ServiceVO saveOrUpdateGoods(@RequestParam(value = "goodsId",required = false)Integer goodsId, Goods goods){
        goodsService.saveOrUpdate(goods,goodsId);
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
    }
    @ApiOperation("删除商品")
    @PostMapping("delete")
    public ServiceVO deleteById(@RequestParam("goodsId")Integer goodsId){
        return goodsService.deleteById(goodsId);
    }

    @ApiOperation("无库存的商品列表展示(可以规范技术商品名称或者编码查询)")
    @PostMapping("/getNoInventoryQuantity")
    public String queryNoInventoryQuantity(@RequestParam("page") Integer page,
                                           @RequestParam("rows")Integer rows,
                                           @RequestParam(value = "nameOrCode",required = false)String nameOrCode){
        Map<String,Object> map = goodsService.queryNoInventoryQuantity(page,rows,nameOrCode);
        return JSON.toJSONString(map);
    }

    @ApiOperation("有库存的商品列表展示(可以规范技术商品名称或者编码查询)")
    @PostMapping("/getHasInventoryQuantity")
    public String queryHasInventoryQuantity(@RequestParam("page") Integer page,
                                           @RequestParam("rows")Integer rows,
                                           @RequestParam(value = "nameOrCode",required = false)String nameOrCode){
        Map<String,Object> map = goodsService.queryHasInventoryQuantity(page,rows,nameOrCode);
        return JSON.toJSONString(map);
    }

    @ApiOperation("添加库存,修改数量或成本价")
    @PostMapping("saveStock")
    public ServiceVO saveStockOrQuantityOrPrice(@RequestParam("goodsId")Integer goodsId,
                                                @RequestParam("inventoryQuantity")Integer inventoryQuantity,
                                                @RequestParam("purchasingPrice")double purchasingPrice){
        goodsService.saveStockOrQuantityOrPrice(goodsId,inventoryQuantity,purchasingPrice);
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
    }
    @ApiOperation("删除库存(要判断上别状态 入库,有间或和销售单据的不能删除")
    @PostMapping("deleteStock")
    public ServiceVO deleteStock(@RequestParam("goodsId")Integer goodsId){
        return goodsService.deleteStock(goodsId);
    }

    @ApiOperation("查询所有 当前库存量 小于 库存下限的商品信息")
    @PostMapping("listAlarm")
    public String listAlarm(){
        Map<String,Object> map = goodsService.listAlarm();
        return JSON.toJSONString(map);
    }
    /**
     * 分页查询商品信息
     * @param page 当前页
     * @param rows 每页显示条数
     * @param goodsName 商品名称
     * @param goodsTypeId 商品类别ID
     * @return
     */


    /**
     * 生成商品编码
     * @return
     */
    @RequestMapping("/getCode")
    @RequiresPermissions(value = "商品管理")
    public ServiceVO getCode() {
        return goodsService.getCode();
    }

    /**
     * 添加或修改商品信息
     * @param goods 商品信息实体
     * @return
     */

    /**
     * 删除商品信息
     * @param goodsId 商品ID
     * @return
     */

    /**
     * 分页查询无库存商品信息
     * @param page 当前页
     * @param rows 每页显示条数
     * @param nameOrCode 商品名称或商品编码
     * @return
     */


    /**
     * 分页查询有库存商品信息
     * @param page 当前页
     * @param rows 每页显示条数
     * @param nameOrCode 商品名称或商品编码
     * @return
     */


    /**
     * 添加商品期初库存
     * @param goodsId 商品ID
     * @param inventoryQuantity 库存
     * @param purchasingPrice 成本价
     * @return
     */

    /**
     * 删除商品库存
     * @param goodsId 商品ID
     * @return
     */

    /**
     * 查询库存报警商品信息
     * @return
     */

}
